import time

from selenium import webdriver
from selenium.webdriver.common.by import By
# 键盘操作
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from handle_excel import HandleExcel

def get_zhibodapan_data(days=None):
    driver = webdriver.Chrome()  # 实例化谷歌浏览器
    driver.maximize_window()  # 最大化浏览器
    driver.get('http://livedashboard.corp.hongsong.club/qualityMarker/rangeLiveData')  # 打开直播大盘--质量大盘--看课数据
    driver.implicitly_wait(10)  # 设置10秒隐式等待
    # time.sleep(1)
    # # 输入账号
    # driver.find_element(By.XPATH,
    #                     '//*[@id="app"]/div/div/div[2]/div/div/div[2]/div/form/div[1]/div/div[1]/input').send_keys(
    #     'fanchao')
    # # 输入密码
    # driver.find_element(By.XPATH,
    #                     '//*[@id="app"]/div/div/div[2]/div/div/div[2]/div/form/div[2]/div/div[1]/input').send_keys(
    #     'fanchao110.')
    # # 点击登录按钮
    # driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/div/div[2]/div/form/div[3]/div/button').click()
    # time.sleep(2)
    # # 点击质量大盘
    # driver.find_element(By.XPATH, '//*[@id="root"]/div/section/aside/div/div[2]/ul/li[4]/div').click()
    # time.sleep(2)
    # # 点击看课数据
    # driver.find_element(By.XPATH, '//*[@id="rc-menu-uuid-19218-2-/qualityMarker-popup"]/li[1]/span/div').click()
    # time.sleep(2)


    # 定义行号
    row_num = 2

    if days is None:  # 如果不传参，默认用当天的日期
        days = [time.strftime('%Y-%m-%d')]

    for day in days:
        # todo 时间筛选
        time_select = driver.find_elements(By.CSS_SELECTOR, '[placeholder="请选择日期"]')  # 定位到时间框，两个，开始时间和结束时间
        for i in range(2):  # 循环两次，一次对开始时间赋值，一次对结束时间赋值
            for j in range(20):  # 删除当前输入框的初始时间，模拟键盘退格操作。
                time_select[i].send_keys(Keys.BACKSPACE)
            time_select[i].send_keys(day)  # 输入指定日期
            time_select[i].send_keys(Keys.ENTER)  # 模拟键盘回车键确定
            time.sleep(1)

        # 点击搜索按钮
        driver.find_element(By.CSS_SELECTOR, '[class="ant-btn css-k7429z ant-btn-primary"]').click()  # 点击搜索按钮
        print('搜索已点击，加载中。。。')

        hde = HandleExcel()  # 实例化excel处理工具类
        # 获取['直播到课人数', '总卡顿秒数', '持续卡顿秒数']等字段
        list_value_int = ['直播到课人数', '总卡顿秒数', '持续卡顿秒数', '10s以下卡顿用户数', '10-50s卡顿用户数',
                          '50s以上卡顿用户数', '3次以上卡顿用户数']
        list_live_data = []
        for i in list_value_int:
            var = WebDriverWait(driver, 1000).until(
                lambda x: x.find_element(By.XPATH,
                                         '//*[text()="%s"]/..//*[@class="ant-statistic-content-value-int"]' % i)).text
            # # 下面是计算百分比的，这三种不需要计算了。
            # if i in ['10s以下卡顿用户数', '10-50s卡顿用户数', '50s以上卡顿用户数']:
            #     var = var.replace(',', '')
            #     a = list_live_data[0]
            #     a = a.replace(',', '')
            #     lv = '%0.2f%%' % (int(var) / int(a) * 100)
            if i in ['3次以上卡顿用户数']:
                var = var.replace(',', '')
                a = list_live_data[0]
                a = a.replace(',', '')
                var = '%0.2f%%' % (int(var) / int(a) * 100)
            list_live_data.append(var)
        # 获取['直播平均看课时长', '总卡顿率', '持续卡顿占比']字段
        list_value = ['直播平均看课时长', '总卡顿率', '持续卡顿占比']
        for i in list_value:
            var = WebDriverWait(driver, 1000).until(
                lambda x: x.find_element(By.XPATH,
                                         '//*[text()="%s"]/..//*[@class="ant-statistic-content-value"]' % i)).text
            list_live_data.append(var)

        print(list_live_data)

        driver.save_screenshot("files/screenshot_%s.png" % day)  # 数据出现后，截图

        hde.excel_write_data(row_num, 1, day)  # 写入日期到excel
        hde.excel_write_data(row_num, 2, list_live_data[0])  # 写入直播到课人数到excel
        hde.excel_write_data(row_num, 4, list_live_data[1])  # 写入总卡顿秒数到excel
        hde.excel_write_data(row_num, 5, list_live_data[2])  # 写入持续卡顿秒数到excel
        hde.excel_write_data(row_num, 7, list_live_data[3])  # 写入10s以下卡顿率到excel
        hde.excel_write_data(row_num, 8, list_live_data[4])  # 写入10-50s卡顿率到excel
        hde.excel_write_data(row_num, 9, list_live_data[5])  # 写入50s以上卡顿率到excel
        hde.excel_write_data(row_num, 10, list_live_data[6])  # 写入3次以上卡顿率到excel
        hde.excel_write_data(row_num, 3, list_live_data[7])  # 写入直播平均看课时长到excel
        hde.excel_write_data(row_num, 6, list_live_data[8])  # 写入总卡顿率到excel
        hde.excel_write_data(row_num, 15, list_live_data[9])  # 写入持续卡顿占比到excel
        # 一条数据处理完毕，行号+1
        row_num += 1
        time.sleep(3)


if __name__ == '__main__':
    riqi = ['2023-08-25', '2023-08-26']
    get_zhibodapan_data()
